package com.example.communitypro.service.impl;


import com.example.communitypro.pojo.UserSearchHistory;
import com.example.communitypro.service.UserSearchHistoryService;
import com.example.communitypro.mapper.UserSearchHistoryMapper;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserSearchHistoryServiceImpl implements UserSearchHistoryService {
    @Autowired(required=false)
    private UserSearchHistoryMapper userSearchHistoryMapper;
    @Override
    public Result addUserSearchHistory(UserSearchHistory userSearchHistory) {
        try {
            if(userSearchHistoryMapper.insertSelective(userSearchHistory)>0)
            {
                return Result.ok().message("新增搜索历史成功");
            }
            else{
                return Result.error().message("新增搜索历史失败");
            }
        }catch (Exception e){
            return Result.error().message("新增搜索历史失败");
        }
    }

    @Override
    public Result dropUserSearchHistory(int search_history_no) {
        try {
            if(userSearchHistoryMapper.deleteByPrimaryKey(search_history_no)>0)
            {
                return Result.ok().message("删除搜索历史成功");
            }
            else{
                return Result.error().message("删除搜索历史失败");
            }
        }catch (Exception e){
            return Result.error().message("删除搜索历史失败");
        }
    }

    @Override
    public Result modifyUserSearchHistory(UserSearchHistory record) {
        try {
            if(userSearchHistoryMapper.updateByPrimaryKeySelective(record)>0)
            {
                return Result.ok().message("修改成功");
            }
            else{
                return Result.error().message("修改失败：未找到对应记录");
            }
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应记录");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result queryUserSearchHistory(int search_history_no) {
        try {
            UserSearchHistory userSearchHistory=userSearchHistoryMapper.selectByPrimaryKey(search_history_no);
            if(userSearchHistory!=null)
            {
                return Result.ok().message("查找搜索历史成功").data("userSearchHistory",userSearchHistory);
            }
            else{
                return Result.error().message("查找搜索历史失败");
            }
        }catch (Exception e){
            return Result.error().message("查找搜索历史失败");
        }
    }

    @Override
    public Result queryUserSearchHistoryServiceByUser(int user_no) {
        try {
            List<UserSearchHistory> userSearchHistories=userSearchHistoryMapper.selectByUser(user_no);
            if(userSearchHistories!=null)
            {
                return Result.ok().message("查找搜索历史成功").data("userSearchHistories",userSearchHistories);
            }
            else{
                return Result.error().message("查找搜索历史失败");
            }
        }catch (Exception e){
            return Result.error().message("查找搜索历史失败");
        }
    }

    @Override
    public Result dropUserSearchHistoryServiceByUser(int user_no) {
        try {
            if(userSearchHistoryMapper.deleteByUser(user_no)>0)
            {
                return Result.ok().message("删除搜索历史成功");
            }
            else{
                return Result.error().message("删除搜索历史失败");
            }
        }catch (Exception e){
            return Result.error().message("删除搜索历史失败");
        }
    }
}
